package fr.emse.tatiana.commands;



import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;

import org.eclipse.ui.IWorkbenchWindow;


/**
 * Create a new corpus - asks for a name and creates a project
 * 
 * @author dyke
 *
 */
public class NewCorpusAction extends Action {

	private final IWorkbenchWindow window;
	private final String viewID;

	public NewCorpusAction(IWorkbenchWindow window, String label, String id) {
		this.window = window;
		this.viewID = id;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_NEW_CORPUS);
		setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
	}


	@Override
	public void run() {
		if(window != null) {	
			InputDialog dialog = new InputDialog(window.getShell(), 
						"New Corpus", 
						"Name of new corpus:", null, null); 
			if (dialog.open() != Window.OK)
				return;
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(dialog.getValue());
			if (!project.exists()) {
				try {
					project.create(null);
					project.open(null);
					project.refreshLocal(IResource.DEPTH_ZERO, null);
				} catch (CoreException e) {
					// won't happen
					e.printStackTrace();
				}
			} else {
				MessageDialog.openError(window.getShell(),"Create Project","Couldn't create project because a project of that name already exists");
			}
		}
	}

}
